class Solution
{
public:
    int numberOfSubstrings(string s)
    {
        int lastOccur[3] = {-1, -1, -1};
        int result = 0;
        int n = s.size();
        for (int i = 0; i < n; ++i)
        {
            lastOccur[s[i] - 'a'] = i;
            if (lastOccur[0] != -1 && lastOccur[1] != -1 && lastOccur[2] != -1)
            {
                result += *min_element(lastOccur, lastOccur + 3) + 1;
            }
        }
        return result;
    }
};